﻿using CMS.BaseEntity;
using ElectronicSelling.BusinessLogic;
using ElectronicSelling.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicSelling.UserControl
{
    public partial class EditCustomerInfo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindCustomerInfo();
        }

        protected void BindCustomerInfo()
        {
            try
            {
                User customer = new User();
                customer.UserId = int.Parse(Session["UserId"].ToString().Trim());
                DataTable dt = HomeAccountLogic.GetCustomerDetail(customer);
                DataRow dr = dt.Rows[0];

                txtCusId.Text = dr["UserId"].ToString();
                txtUserName.Text = dr["UserName"].ToString();
                txtAddress.Text = dr["Address"].ToString();
                txtDOB.Text = dr["DateOfBirth"].ToString();
                txtEmail.Text = dr["Email"].ToString();
                txtFullName.Text = dr["FullName"].ToString();
                txtPhone.Text = dr["Phone"].ToString();
                txtPhonePreventive.Text = dr["PhonePreventive"].ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            User customer = new User();
            customer.UserId = int.Parse(Session["UserId"].ToString());
            customer.Address = txtAddress.Text;
            customer.DateOfBirth = txtDOB.Text;
            customer.Email = txtEmail.Text;
            customer.FullName = txtFullName.Text;
            customer.Phone = txtPhone.Text;
            customer.PhonePreventive = txtPhonePreventive.Text;
            if (radioGender.SelectedValue == "1")
                customer.Gender = "True";
            else
                customer.Gender = "False";
            //if (HomeAccountLogic.IsUpdateCustomer(customer))
            //{

            //}

            SqlConnection con = Connection.GetConnection();
            con.Open();
            string str = "update [User] set FullName='" + customer.FullName + "',  Gender='" + customer.Gender + "', Phone='" + customer.Phone + "', PhonePreventive='" + customer.PhonePreventive + "', DateOfBirth='" + customer.DateOfBirth + "', Email='" + customer.Email + "', Address='" + customer.Address + "' where UserId='" + customer.UserId + "' ";
            SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}